package com.automailerbot.alias;

import com.automailerbot.utils.WebDriverFactory;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.stereotype.Service;

@Service
public class OutlookAliasManager {

    public void addAlias(String email, String password, String aliasPrefix) throws Exception {
        WebDriver driver  = WebDriverFactory.createDriver();

        try {
            driver.get("https://login.live.com/");
            driver.findElement(By.name("loginfmt")).sendKeys(email);
            driver.findElement(By.id("idSIButton9")).click();
            Thread.sleep(2000);

            driver.findElement(By.name("passwd")).sendKeys(password);
            driver.findElement(By.id("idSIButton9")).click();
            Thread.sleep(3000);

            driver.get("https://account.live.com/names/manage");
            Thread.sleep(3000);

            WebElement aliasField = driver.findElement(By.name("NewAlias"));
            aliasField.sendKeys(aliasPrefix + "@outlook.com");
            driver.findElement(By.id("AddAliasButton")).click();

            Thread.sleep(5000); // 等待添加完成
            System.out.println("✅ 别名添加成功");

        } catch (Exception e) {
            throw new Exception("添加别名失败: " + e.getMessage(), e);
        } finally {
            driver.quit();
        }
    }
}
